﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;
using PagedList;

namespace ShenLanCMS.Web.Controllers
{
    public class MsgboardController : Controller
    {
        private EntityContext db = new EntityContext();

        int pageSize = 20;

        public ViewResult Index()
        {
            return View();
        }

        //管理友情链接
        [IsAdmin]
        public ViewResult Admin(int? page)
        {
            var index = (page ?? 1) - 1;
            ViewData["actionName"] = "Admin";
            var list = db.Msgboards.ToPagedList(index, pageSize);
            return View(list);
        }

        [IsAdmin]
        public ViewResult Details(Guid id)
        {
            Msgboard msgboard = db.Msgboards.Find(id);
            return View(msgboard);
        }

        //发表留言
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PostMsg()
        {
            try
            {
                var title = Request.QueryString["title"];
                var nickname = Request.QueryString["nickname"];
                var email = Request.QueryString["email"];
                var siteurl = Request.QueryString["siteurl"];
                //处理网址
                if (!string.IsNullOrEmpty(siteurl))
                {
                    siteurl = siteurl.ToLower();
                    if (siteurl.IndexOf("http://") == -1 && siteurl.IndexOf("https://") == -1)
                        siteurl = "http://" + siteurl;
                }
                var msg = Request.QueryString["msg"];
                var msgboard = new Msgboard();
                msgboard.MsgboardID = Guid.NewGuid();
                msgboard.Title = title;
                msgboard.MsgTime = DateTime.Now;
                msgboard.Msg = msg;
                msgboard.NickName = nickname;
                msgboard.Email = email;
                msgboard.SiteUrl = siteurl;
                db.Msgboards.Add(msgboard);
                db.SaveChanges();
                return Json("留言成功");
            }
            catch { return Json("留言失败!请确保您的输入不包含特殊字符或标点符号"); }
        }

        //留言
        [HttpPost]
        public ActionResult GetMsgboard()
        {
            try
            {
                var msgboards = db.Msgboards.OrderByDescending(n => n.MsgTime);
                var strHtml = new System.Text.StringBuilder();

                if (msgboards.Count() == 0)
                    return Json("暂无留言");
                else
                {
                    var count = msgboards.Count();
                    foreach (var item in msgboards)
                    {
                        strHtml.Append("<div><span>第" + count + "楼  说:" + item.Title);
                        strHtml.Append("<br/>----------------------------------------<br/>");
                        strHtml.Append(item.Msg);
                        strHtml.Append("</div><br/>");
                        strHtml.Append("<div><span><a href='mailto:" + item.Email + "'>" + item.NickName + "</a>  发表于:" + item.MsgTime + "</span>");
                        strHtml.Append(" <a href='" + item.SiteUrl + "' target='_blank'>TA的主页</a></div>");
                        strHtml.Append("<hr/><br/><br/>");
                        count--;
                    }
                    return Json(strHtml.ToString());
                }
            }
            catch { return Json("获取留言出错"); }
        }

        [IsAdmin]
        public ActionResult Edit(Guid id)
        {
            Msgboard msgboard = db.Msgboards.Find(id);
            return View(msgboard);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(Msgboard msgboard)
        {
            if (ModelState.IsValid)
            {
                db.Entry(msgboard).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Admin");
            }
            return View(msgboard);
        }

        [HttpPost]
        [IsAdmin]
        public ActionResult Delete(Guid id)
        {
            try
            {
                Msgboard msgboard = db.Msgboards.Find(id);
                db.Msgboards.Remove(msgboard);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}